\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fp-pas}[1994/08/29]

%version information
\def\FP@pasversion{1.0}
\message{%
  `Fixed Point Pascal Triangle',%
  \space\space%
  Version \FP@pasversion%
  \space(C) Michael Mehlich%
}

%resolve dependencies
\RequirePackage{fp-basic}

%%%public area (macros which may be used)%%%

\def\FPpascal#1#2{\FP@pascal{#1}{#2}}%pascal triangle for lines 0,...,63

%%%private fp-area (don't use these macros)%%%

%allocation of registers
\countdef\FP@pas=50
\countdef\FP@pasc=51

%addition of two natural numbers
\def\FP@pasadd#1#2#3{%
  % #1 macro, which gets the result
  % #2 1st value
  % #3 2nd value
  {\FP@removeleadingzeros#2\relax%
   \expandafter\FP@setintcounter\FP@tmp000000000000000000\relax%
   \FP@xia=\FP@rega%
   \FP@xib=\FP@regb%
   %
   \FP@removeleadingzeros#3\relax%
   \expandafter\FP@setintcounter\FP@tmp000000000000000000\relax%
   \FP@yia=\FP@rega%
   \FP@yib=\FP@regb%
   %
   \advance\FP@xia\FP@yia%
   \advance\FP@xib\FP@yib%
   \ifnum\FP@xib<1000000000\relax\else%
     \advance\FP@xia1\relax%
     \advance\FP@xib-1000000000\relax%
   \fi%
   \ifnum\FP@xia<1000000000\relax\else%
     \FP@errmessage{Overflow}%
   \fi%
   %
   \ifnum\FP@xia=0\relax%
     \edef\FP@tmpa{\the\FP@xib}%
   \else%
     \advance\FP@xib1000000000%
     \edef\FP@tmpa{\the\FP@xia\expandafter\FP@ignorenext\the\FP@xib}%
   \fi%
   %
   \global\let\FP@tmpa\FP@tmpa%
  }%
  %
  \let#1\FP@tmpa%
}

%the first two entries of the pascal-triangle
\expandafter\edef\csname FP@pas0\endcsname{[1]}
\expandafter\edef\csname FP@pas1\endcsname{[1,1]}

%which is the highest line of the pascal-triangle we have already computed?
\expandafter\edef\csname FP@pascount\endcsname{1}

%compute next line of pascal-triangle
\def\FP@pasdouble[#1]{%
  % #1 previous line of pascal-triangle
  \edef\FP@old{}%
  \edef\FP@tmpb{}%
  \@for\FP@new:=#1\do{%
    \expandafter\if!\FP@old!\relax%
      \edef\FP@old{\FP@new}%
    \else%
      \FP@pasadd{\FP@tmpa}{\FP@old}{\FP@new}%
      \edef\FP@tmpb{\FP@tmpb,\FP@tmpa}%
      \edef\FP@old{\FP@new}%
    \fi%
  }%
  \global\edef\FP@tmpb{[1\FP@tmpb,1]}%
}

%compute n-th line of pascal-triangle
\def\FP@pascomp#1{%
  % #1 which line to compute
  \expandafter\FP@pasc\FP@pascount\relax%
  \ifnum\FP@pasc<#1\relax%
    \loop%
      \ifnum\FP@pasc<#1\relax%
        \expandafter\expandafter\expandafter\FP@pasdouble\csname FP@pas\the\FP@pasc\endcsname%
        \advance\FP@pasc1\relax%
	\edef\next{\noexpand\global\noexpand\let\csname FP@pas\the\FP@pasc\endcsname\noexpand\FP@tmpb}%
	\next%
      \repeat%
  \fi%
  \global\edef\FP@pascount{\the\FP@pasc}%
}

\def\FP@pascal#1#2{%
  % #1 macro, which gets the result
  % #2 which line of the pascal-triangle to compute
  {\FP@beginmessage{PASCAL}%
   %
   \FP@pas#2\relax%
   \FP@pascomp\FP@pas%
   \global\edef\FP@tmp{\csname FP@pas\the\FP@pas\endcsname}%
   %
   \FP@endmessage{}%
  }%
  \let#1\FP@tmp%
}
